﻿using System.Data;
using System.Data.SqlClient;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Symantec.DataFactories;
using System;

namespace DataFactoriesTests
{

    /// <summary>
    ///This is a test class for SqlClientDataFactoryTest and is intended
    ///to contain all SqlClientDataFactoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SqlClientDataFactoryTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public const string SQLCONNECTIONSTRING = "Data Source=TUS1SYMDBSDIN01\\SYMDBDEV,1433;Network=DBMSSOCN;Initial Catalog=WebSpt;uid=bar_webuser;password=chansle_114234;Persist Security Info=True;";

        /// <summary>
        ///A test for SqlClientDataFactory Constructor
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Symantec.DataFactories.dll")]
        public void SqlClientDataFactoryConstructorTest()
        {
            SqlClientDataFactory_Accessor target = new SqlClientDataFactory_Accessor();
            Assert.IsNotNull(target);

            Assert.IsNull(target._connection);
            target.CreateConnection();
            Assert.IsInstanceOfType(target._connection, typeof(SqlConnection));

            Assert.IsNull(target._command);
            target.CreateCommand();
            Assert.IsInstanceOfType(target._command, typeof(SqlCommand));

            Assert.IsNull(target._dataAdapter);
            target.CreateDataAdapter();
            Assert.IsInstanceOfType(target._dataAdapter, typeof(SqlDataAdapter));
        }

        /// <summary>
        ///A test for CreateCommand
        ///</summary>
        [TestMethod()]
        public void CreateCommandTest()
        {
            SqlClientDataFactory_Accessor target = new SqlClientDataFactory_Accessor();
            IDbCommand expected = new SqlCommand();
            IDbCommand actual;
            actual = target.CreateCommand();
            Assert.IsInstanceOfType(actual, expected.GetType());
        }

        /// <summary>
        ///A test for CreateConnection
        ///</summary>
        [TestMethod()]
        public void CreateConnectionTest()
        {
            SqlClientDataFactory_Accessor target = new SqlClientDataFactory_Accessor();
            IDbConnection expected = new SqlConnection();
            IDbConnection actual;
            actual = target.CreateConnection();
            Assert.IsInstanceOfType(actual, expected.GetType());
        }

        /// <summary>
        ///A test for CreateDataAdapter
        ///</summary>
        [TestMethod()]
        public void CreateDataAdapterTest()
        {
            SqlClientDataFactory_Accessor target = new SqlClientDataFactory_Accessor();
            IDataAdapter expected = new SqlDataAdapter();
            IDataAdapter actual;
            actual = target.CreateDataAdapter();
            Assert.IsInstanceOfType(actual, expected.GetType());
        }

        /// <summary>
        ///A test for ExecuteXmlReader
        ///</summary>
        [TestMethod()]
        public void ExecuteXmlReaderTest()
        {
            SqlClientDataFactory_Accessor target = new SqlClientDataFactory_Accessor();
            XmlReader actual;

            // ExecuteXmlReader requires functioning connection and command objects
            IDbConnection connection = target.CreateConnection();
            connection.ConnectionString = SQLCONNECTIONSTRING;

            using (connection)
            {
                IDbCommand command = target.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = "select * from SymUsers for xml auto";

                connection.Open();
                actual = target.ExecuteXmlReader();
            }
            Assert.IsInstanceOfType(actual, typeof(XmlReader));
        }

        /// <summary>
        ///A test for Instance
        ///</summary>
        [TestMethod()]
        public void InstanceTest()
        {
            IDataFactory actual;
            actual = SqlClientDataFactory.Instance;
            Assert.IsInstanceOfType(actual, typeof(SqlClientDataFactory));
        }

        /// <summary>
        ///A test for SupportsXmlReader
        ///</summary>
        [TestMethod()]
        public void SupportsXmlReaderTest()
        {
            SqlClientDataFactory_Accessor target = new SqlClientDataFactory_Accessor(); 
            bool actual;
            actual = target.SupportsXmlReader;
            Assert.IsTrue(actual);
        }

        /// <summary>
        ///A test for ExecuteReader
        ///</summary>
        [TestMethod()]
        public void ExecuteReaderTest()
        {
            SqlClientDataFactory_Accessor target = new SqlClientDataFactory_Accessor();
            IDataReader actual;

            // ExecuteXmlReader requires functioning connection and command objects
            IDbConnection connection = target.CreateConnection();
            connection.ConnectionString = SQLCONNECTIONSTRING;

            using (connection)
            {
                IDbCommand command = target.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = "select * from SymUsers for xml auto";

                connection.Open();
                actual = target.ExecuteReader();
            }
            Assert.IsInstanceOfType(actual, typeof(IDataReader));
        }

        /// <summary>
        ///A test for Fill
        ///</summary>
        [TestMethod()]
        public void FillTest()
        {
            SqlClientDataFactory_Accessor dataFactory = new SqlClientDataFactory_Accessor();

            // Test that the Symantec.DataFactories.AdomdClientDataFactory fills a DataTable and returns
            // the number of rows added to that DataTable.
            IDbConnection connection = dataFactory.CreateConnection();
            connection.ConnectionString = SQLCONNECTIONSTRING;
            DataSet dsActual = new DataSet();
            int iTarget, iActual;

            using (connection)
            {
                IDbCommand command = dataFactory.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = "select * from SymUsers";

                IDataAdapter dataAdapter = dataFactory.CreateDataAdapter();
                iActual = dataFactory.Fill(dsActual);
            }

            iTarget = dsActual.Tables[0].Rows.Count;
            Assert.IsTrue(iActual > 0);
            Assert.AreEqual(iTarget, iActual);
        }
    }
}
